package com.example.wan1.view.system.knowledge;

import android.content.Intent;
import android.os.Bundle;

import com.example.wan1.R;
import com.example.wan1.adapter.KnowledgeAdapter;
import com.example.wan1.base.fragment.SimpleFragment;
import com.example.wan1.model.KnowledgeBean;
import com.example.wan1.view.system.knowledge.level2.KnowledgeLevelActivity;
import com.scwang.smart.refresh.header.MaterialHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import butterknife.BindView;

/***
 * 创建时间：2020/7/8 21:46
 * 创建人：10850
 * 功能描述：知识体系界面
 */
public class KnowledgeSystemFragment extends SimpleFragment implements KnowledgeContract.View,
        OnRefreshListener {

    @BindView(R.id.srlheader)
    MaterialHeader mSrlheader;
    @BindView(R.id.system_rv)
    RecyclerView mSystemRv;
    @BindView(R.id.srl)
    SmartRefreshLayout mSrl;
    @Inject
    List<KnowledgeBean> mList;
    @Inject
    KnowledgeAdapter mAdapter;

    //子页面-标题与id的集合
    private List<String> mChildTitles;
    private List<Integer> mChildIds;

    @Inject
    KnowledgePresenter mPresenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //mList = new ArrayList<>();
        mChildTitles = new ArrayList<>();
        mChildIds = new ArrayList<>();
    }

    @Override
    protected void initData() {
        mPresenter.getKnowledgeTreeData();
    }

    @Override
    protected void initView() {
        showLoading();
        //mSrlheader.setColorSchemeResources(R.color.qc_green);
        mSrl.setEnableLoadMore(false);
        mSrl.setEnableRefresh(false);

        //mAdapter = new KnowledgeAdapter(R.layout.knowledge_rv_item, mList);
        mSystemRv.setAdapter(mAdapter);
        mSystemRv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));

        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            if (mChildTitles.size() > 0) {
                mChildTitles.clear();
                mChildIds.clear();
            }
            Intent intent = new Intent(getActivity(), KnowledgeLevelActivity.class);
            intent.putExtra("title", mList.get(position).getName());
            for (int i = 0; i < mList.get(position).getChildren().size(); i++) {
                mChildTitles.add(mList.get(position).getChildren().get(i).getName());
                mChildIds.add(mList.get(position).getChildren().get(i).getId());
            }
            intent.putStringArrayListExtra("list", (ArrayList<String>) mChildTitles);
            intent.putIntegerArrayListExtra("ids", (ArrayList<Integer>) mChildIds);
            startActivity(intent);
        });
        mSystemRv.addOnScrollListener(mOnScrollListener);
    }

    @Override
    protected int initLayoutRes() {
        return R.layout.fragment_knowledge;
    }

    @Override
    public void onSuccess(List<KnowledgeBean> data) {
        hideLoading();
        mSrl.setEnableRefresh(true);
        mAdapter.setNewData(data);
        mList.addAll(data);
    }

    @Override
    public void onFailure(String msg) {
        showError();
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        mSystemRv.postDelayed(() -> {
            mPresenter.getKnowledgeTreeData();
            mSrl.finishRefresh(true);
        }, 200);
    }


}
